﻿// /////////////////////////////////// 
//  
//  
//      WiseLink CCF2.0 
//     (c)Copyright 2001-2010 WiseLink Xiaojun (David) Chen 
//        Authored and Owned Xiaojun (David) Chen 
//        All Right Reserved
//  
// /////////////////////////////////// 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WiseLink.RDS.Model.Common;
using System.Web.Mvc;

namespace WiseLink.RDS.Model
{
    public class CustomerRepository :RDSRepositoryBase
    {
        public CustomerRepository() : base()
        {

        }

        public IQueryable<CustomerViewModel> GetAllCustomers()
        {
            var query = from c in ccf20Ent.Customers
                        select new CustomerViewModel
                        {

                            CustomerID = c.CustomerID,
                            DeviceID = c.Devices.FirstOrDefault() == null ?0:c.Devices.FirstOrDefault().DeviceID,
                            CustomerNo = c.CustomerNo,
                            Gender = c.Gender,
                            IdentityNo = c.IdentityNo,
                            Name = c.Name,
                            PrimaryPhone = c.FirstMobile,
                            SecondaryPhone = c.SecondMobile,
                            ThirdaryPhone = c.ThirdMobile,
                            VehicleModel = c.Vehicles.FirstOrDefault().VehicleType.Manufacture + "-" + 
                                           c.Vehicles.FirstOrDefault().VehicleType.Model + "-" + 
                                           c.Vehicles.FirstOrDefault().VehicleType.EngineType + "-" + 
                                           c.Vehicles.FirstOrDefault().VehicleType.ModelYear,
                            ChassiNo = c.Vehicles.FirstOrDefault().ChassiNo,
                            PurchaseRegion = c.Vehicles.FirstOrDefault().Location,
                            VehicleLicensePlate = c.Vehicles.FirstOrDefault().VehicleLicense,
                            VehiclePurchaseDate = c.Vehicles.FirstOrDefault().SaleDate,
                            VehicleTypeID = c.Vehicles.FirstOrDefault().VehicleTypeID,
                            CurrentMileage = c.Vehicles.FirstOrDefault().CurrentMileage,
                            EngineNo = c.Vehicles.FirstOrDefault().EngineNo
                            

                        };
            return query;
        }

        public void Insert(CustomerViewModel customerVM)
        {
            Customer customer = new Customer();
            customer.Name = customerVM.Name;
            customer.FirstMobile = customerVM.PrimaryPhone;
            ccf20Ent.Customers.AddObject(customer);
            ccf20Ent.SaveChanges();
        }

        public void Delete(int id)
        {
            Customer customer = ccf20Ent.Customers.Where(c => c.CustomerID == id).FirstOrDefault();
            if (customer != null)
            {
                
                TransferedCustomer transfer = customer.TransferedCustomers.FirstOrDefault();
                Vehicle vehicle = customer.Vehicles.FirstOrDefault();
                ccf20Ent.Customers.DeleteObject(customer);

                if (transfer != null)
                {
                    ccf20Ent.TransferedCustomers.DeleteObject(transfer);
                }
                if (vehicle != null)
                {
                    ccf20Ent.Vehicles.DeleteObject(vehicle);
                }
                ccf20Ent.SaveChanges();
            }
        }

        public void Update(CustomerViewModel updCustomer)
        {
            Customer customer = ccf20Ent.Customers.Where(c => c.CustomerID == updCustomer.CustomerID).FirstOrDefault();
            customer.Name = updCustomer.Name;
            customer.FirstMobile = updCustomer.PrimaryPhone;
            customer.SecondMobile = updCustomer.SecondaryPhone;
            customer.ThirdMobile = updCustomer.ThirdaryPhone;
            customer.CustomerNo = updCustomer.CustomerNo;
            customer.Gender = updCustomer.Gender;
            customer.IdentityNo = updCustomer.IdentityNo;

            customer.Vehicles.FirstOrDefault().Location = updCustomer.PurchaseRegion;
            customer.Vehicles.FirstOrDefault().SaleDate = updCustomer.VehiclePurchaseDate;
            if (updCustomer.VehicleTypeID > 0)
            {
                customer.Vehicles.FirstOrDefault().VehicleTypeID = updCustomer.VehicleTypeID;
            }
            ccf20Ent.SaveChanges(); 
        }
        public CustomerViewModel GetOneCustomer(int id)
        {
            return GetAllCustomers().Where(c => c.CustomerID == id).FirstOrDefault();
        }

        public SelectList GetCustomerName()
        {
            var query = from c in GetAllCustomers()
                        where c.DeviceID ==0
                        select new { CustomerID = c.CustomerID, CustomerName = c.Name };
            return new SelectList(query.ToList(), "CustomerID", "CustomerName");
        }
    }
}
